package com.sale.system.service.impl;

import com.sale.common.utils.StringUtils;
import com.sale.common.utils.bean.BeanUtils;
import com.sale.common.utils.ftp.SftpClientUtil;
import com.sale.system.domain.Monitor;
import com.sale.system.domain.vo.MonitorItemVo;
import com.sale.system.domain.vo.MonitorVO;
import com.sale.system.mapper.MonitorMapper;
import com.sale.system.service.IMonitorService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 监控实现
 *
 * @author zhangyingdong
 * @version 1.0.0
 * @package com.sale.system.service.impl
 * @className MonitorServiceImpl
 * @date 2022-02-17  18:19
 */
@Service
public class MonitorServiceImpl implements IMonitorService {

    @Resource
    private MonitorMapper monitorMapper;


    @Override
    public List<MonitorVO> watchList(Monitor monitor) {
        List<MonitorVO> monitorVOList = new ArrayList<MonitorVO>();
        List<Monitor> list = monitorMapper.selectList(monitor);
        if (list != null && list.size()>0) {
            for (Monitor monitor1 : list) {
                MonitorVO monitorVO = new MonitorVO();
                BeanUtils.copyProperties(monitor1, monitorVO);
                String monitorResult = null;
                try {
                    monitorResult = SftpClientUtil.monitor(monitor1.getIp(), monitor1.getPort(), monitor1.getUsername(), monitor1.getPassword());
                } catch (Exception e) {
                    e.printStackTrace();
                }
                if(StringUtils.isEmpty(monitorResult)) {
                    continue;
                }
                if (StringUtils.isNotEmpty(monitorResult)) {
                    String[] arr = monitorResult.split("\\s+");
                    List<String> list1 = null;
                    List<String> list2 = null;
                    List<String> list3 = null;
                    List<String> list4 = null;
                    List<String> list5 = null;
                     List<String> list6 = null;
                     List<String> list7 = null;
                     List<String> list8 = null;
                     List<String> list9 = null;
                     List<String> list10 = null;

                     if (monitor1.getIp().contains("112")) {
                        list1 = convertList(arr, 0, 5);
                        list2 = convertList(arr, 6, 11);
                        list3 = convertList(arr, 12, 17);
                        list4 = convertList(arr, 18, 23);
                        list5 = convertList(arr, 24, 29);
                        list6 = convertList(arr, 30, 35);
                        list7 = convertList(arr, 36, 41);
                        list8 = convertList(arr, 42, 47);
                        list9 = convertList(arr, 48, 53);
                        list10 = convertList(arr, 54, 59);
                    } else if (monitor1.getIp().contains("113")) {
                         list1 = convertList6to5(arr, 0, 6);
                         list2 = convertList(arr, 7, 12);
                         list3 = convertList(arr, 13, 18);
                         list4 = convertList(arr, 19, 24);
                         list5 = convertList(arr, 25, 30);
                         list6 = convertList(arr, 31, 36);
                         list7 = convertList(arr, 37, 42);
                         list8 = convertList(arr, 43, 48);
                         list9 = convertList(arr, 49, 54);
                        list10 = null;
                    }  else if (monitor1.getIp().contains("45")) {
                        list1 = convertList6to5(arr, 0, 6);
                        list2 = convertList(arr, 7, 12);
                        list3 = convertList(arr, 13, 18);
                        list4 = convertList(arr, 19, 24);
                        list5 = convertList(arr, 25, 30);
                        list6 = convertList(arr, 31, 36);
                        list7 = convertList(arr, 37, 42);
                        list8 = convertList(arr, 43, 48);
                        list9 = null;
                        list10 = null;
                    }else {
                        list1 = convertList6to5(arr, 0, 6);
                        list2 = convertList(arr, 7, 12);
                        list3 = convertList(arr, 13, 18);
                        list4 = convertList(arr, 19, 24);
                        list5 = convertList(arr, 25, 30);
                        list6 = convertList(arr, 31, 36);
                        list7 = convertList(arr, 37, 42);
                        list8 = convertList(arr, 43, 48);
                        list9 = convertList(arr, 49, 54);
                        list10 = convertList(arr, 55, 60);
                    }
                    MonitorItemVo itemVo = new MonitorItemVo();
                    itemVo.setList1(list1);
                    itemVo.setList2(list2);
                    itemVo.setList3(list3);
                    itemVo.setList4(list4);
                    itemVo.setList5(list5);
                    itemVo.setList6(list6);
                    itemVo.setList7(list7);
                    itemVo.setList8(list8);
                    itemVo.setList9(list9);
                    itemVo.setList10(list10);
                    monitorVO.setMonitorItemVo(itemVo);
                }
                monitorVO.setMonitorResult(monitorResult);
                monitorVOList.add(monitorVO);
            }
        }
        return monitorVOList;
    }

    private List<String> convertList6to5(String[] arr, int startInd, int endInd) {
        List<String> list = new ArrayList<>();
        for(int i=startInd; i<=endInd-1; i++) {
            if (i < arr.length-1) {
                if (i < endInd-1) {
                    list.add(arr[i]);
                } else if(i == endInd-1){
                    list.add(arr[i]+" "+arr[endInd]);
                }
            }

        }
        return list;
    }

    private List<String> convertList(String[] arr, int startInd, int endInd) {
        List<String> list = new ArrayList<>();
        for(int i=startInd; i<=endInd; i++) {
            if (i <= arr.length-1 && arr[i] != null) {
                list.add(arr[i]);
            }
        }
        return list;
    }
}
